package com.tools.cleanmaster.ui.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;

import com.tools.cleanmaster.R;

/**
 * 通用Activity,用于加载Fragment。
 */
public class BaseFragmentActivity extends BaseActivity {
    public static final String FRAGMENT_NAME = "fragment_name";

    public static void startBaseFragmentActivity(Context context, String fragment) {
        Intent intent = new Intent(context, BaseFragmentActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(FRAGMENT_NAME, fragment);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_fragment_activity);
        gotoPage(getIntent().getStringExtra(FRAGMENT_NAME));
    }

    public boolean gotoPage(String key) {
        if (TextUtils.isEmpty(key)) {
            return false;
        }

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment fragment = Fragment.instantiate(this, key);
        if (fragment == null) {
            return false;
        }
        transaction.replace(R.id.fragment, fragment);
        transaction.commitAllowingStateLoss();
        return true;
    }
}
